// pages/login/login.js

const util = require('../../utils/util');
// const {AuthLoginByWeixin,AuthLoginByAccount,AuthLogout,AuthRegister,AuthReset}=require('../../config/api')
const user = require('../../utils/user')
const app = getApp()

Page({
  data: {
      needback:0,
    
  },

  // 微信直接登录
  loginByWx(e){
    let userInfo = e.detail.userInfo
    if(userInfo==undefined){
      app.globalData.hasLogin = false;
      util.showErrorToast('微信登录失败！')
      return
    }
    // 检验登录态，如果过期需重新登录
    user.checkLogin().catch(()=>{
      user.loginByWeixin(userInfo).then(res=>{
        app.globalData.hasLogin=true;
        wx.navigateBack({
          delta:1
        })
      }).catch(err=>{
        app.globalData.hasLogin=false;
        util.showErrorToast("微信登录失败！")
      })
    })
    
  },

  // 账号登录
  loginByAccount(){
    wx.navigateTo({
      url: this.data.needback?'../loginByAccount/loginByAccount?needback=1':'../loginByAccount/loginByAccount',
    })
    this.setData({
      needback:0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    if(options.needback){
      this.setData({
        needback:options.needback
      })
    }

  },


})